<?php

/**
 * 公共控制器
 * 主要用于权限验证
 */
namespace Admin\Controller;
use Lib\Controller;

class BaseController extends Controller {
	
	
	/**
	 * 初始化
	 */
	function __construct() {
		parent::__construct();
		$this->checkLogin();
		//$this->checkAuth();
	}
	
	
	/**
	 * 检查会话是否超时
	 */
	public function checkLogin(){
		if(!isset($_SESSION['SESSION_USER'])){
			R(__WWWROOT__.'Admin/Login/login.php');
		}
	}
	
	
	/**
	 * 访问权限验证
	 */
	private function checkAuth(){
		$url = 'Role/add.php';
		$role_menus = $_SESSION['SESSION_USER']['role_menus'];
		$isAccess = getInstance('Admin\Model\RoleModel')->authRoleAccess($url, $role_menus);
		if(!$isAccess){
			$message = '抱歉，您没有权限操作。';
			if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'){
				$this->ajaxReturn(300, $message);
			}else{
				$this->redirect('', 3, $message);
			}
		}
	}
	
	
	/**
	 * 上传图片
	 */
	function upload($image_dir, $type='image'){
		import('Util.Upload');
		$upload_boj = new \Upload();
		switch($type){
			case 'image':
				$upload_boj->allowTypes = array('image/gif','image/jpg','image/jpeg', 'image/pjpeg','image/bmp','image/x-png');
				break;
			case 'csv':
				$upload_boj->allowTypes = array('application/vnd.ms-excel', 'application/octet-stream');
				break;		
		}
		$message = $upload_boj->upload($image_dir);
		if($message === false){
			$this->ajaxReturn('300', $upload_boj->getErrorMsg());
		}
		$file_info = $upload_boj->getUploadFileInfo();
		echo '{\'err\':\'\',\'msg\':\''.__WWWROOT__.'Upload/News/'.$file_info[0]['savename'].'\'}';
	}
	
	
	/**
	 * 获得因子内容
	 */
	protected function getFactor($name){
		$factors = getTestFactor($name);
		if(is_array($factors)){
			foreach($factors as $val){
				$list[] = array('name'=>$val['name']);
			}	
		}
		return $list;
	}
	
	/**
	 * 获取测试报告条件sql语句
	 */
	protected function getTestReportSql($test){
		$factors = $this->getFactor($test);
		$sql = null;
		if(is_array($factors)){
			$mark = array(1=>'=', 2=>'>', 3=>'>=', 4=>'<', 5=>'<=');
			foreach($factors as $k=>$f){
				$k++;
				$filed = "{$test}.factor{$k}";
				$one = $_POST["factor{$k}_one"];
				$num1 = (int)$_POST["factor{$k}_num1"];
				$two = $_POST["factor{$k}_two"];
				$three = $_POST["factor{$k}_three"];
				$num2 = (int)$_POST["factor{$k}_num2"];
				if($one){
					$sql .= '(';
					$sql .= "$filed {$mark[$one]} $num1 ";
					if($two && $three){
						$sql .= "$two $filed {$mark[$three]} $num2";
					}
					$sql .= ') OR ';
				}
			}
		}
		if($sql){
			$sql = '('.rtrim($sql, ' OR ').')';
		}
		return $sql;
	}
	
	/**
	* navTabAjaxDone是DWZ框架中预定义的表单提交回调函数．
	* 服务器转回navTabId可以把那个navTab标记为reloadFlag=1, 下次切换到那个navTab时会重新载入内容. 
	* callbackType如果是closeCurrent就会关闭当前tab
	* 只有callbackType="forward"时需要forwardUrl值
	* navTabAjaxDone这个回调函数基本可以通用了，如果还有特殊需要也可以自定义回调函数.
	* 如果表单提交只提示操作是否成功, 就可以不指定回调函数. 框架会默认调用DWZ.ajaxDone()
	* <form action="/user.do?method=save" onsubmit="return validateCallback(this, navTabAjaxDone)">
	* 
	* form提交后返回json数据结构statusCode=DWZ.statusCode.ok表示操作成功, 做页面跳转等操作. statusCode=DWZ.statusCode.error表示操作失败, 提示错误原因. 
	* statusCode=DWZ.statusCode.timeout表示session超时，下次点击时跳转到DWZ.loginUrl
	* {"statusCode":"200", "message":"操作成功", "navTabId":"navNewsLi", "forwardUrl":"", "callbackType":"closeCurrent"}
	* {"statusCode":"300", "message":"操作失败"}
	* {"statusCode":"301", "message":"会话超时"}
	* 
	*/
	function ajaxReturn($statusCode, $message, $navTabId = '', $callbackType = '', $forwardUrl = '') {
		if($statusCode == 200 && $navTabId == null){
			//$navTabId = strtolower('admin');
		}
		$data = array (
			'statusCode' => $statusCode,
			'message' => $message,
			'navTabId' => $navTabId,
			'callbackType' => $callbackType,
			'forwardUrl' => $forwardUrl
		);
		exit (json_encode($data));
	}
	
}
?>